﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Discovery;
using System.ServiceModel.Description;
using System.Net;
using Hardcodet.Wpf.TaskbarNotification;
using WindSim;

namespace WindSimServiceHost
{
    public partial class TrayPart : Window, INotifyPropertyChanged
    {
        //Properties & Fields
        private ServiceHost host;
        private const int port = 8732;
        private Uri hostUri;
        private string _serviceStatus;
        public string ServiceStatus
        {
            get { return _serviceStatus; }
            set { _serviceStatus = value; NotifyPropertyChanged("ServiceStatus"); }
        }

        //Constructor
        public TrayPart()
        {
            InitializeComponent();
            StartService();
        }

        //NotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        //Methods
        private void StartService()
        {
            try
            {
                string hostname = System.Environment.MachineName;
                host =  new ServiceHost(typeof(Simulation));
                
                host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
                host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

                hostUri = new UriBuilder { Scheme = Uri.UriSchemeNetTcp, 
                                            Port = port, 
                                            Host = hostname, 
                                            Path = "SimulationService" }.Uri;
                host.AddServiceEndpoint(typeof(ISimulationService), 
                                        new NetTcpBinding(SecurityMode.None), 
                                        hostUri);

                //Vor Release entfernen///////////////////////////////////////////////////////////////////////////////////////////////////////////////
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.HttpGetUrl = new UriBuilder { Scheme = Uri.UriSchemeHttp, Port = port + 1, Host = hostname, Path = "MetadataExchange" }.Uri;
                host.Description.Behaviors.Add(smb);
                host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), smb.HttpGetUrl);
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                host.Closed += host_StatusChanged;
                host.Faulted += host_StatusChanged;
                host.Opened += host_StatusChanged;

                host.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + System.Environment.NewLine + ex.InnerException);
            }
        }
        private void ShowBalloon()
        {
            string title = "WindSimService an " + hostUri;
            string text = "Status: " + host.State;

            tiServiceStatus.ShowBalloonTip(title, text, BalloonIcon.Info);
        }

        //Eventhandler
        private void host_StatusChanged(object sender, EventArgs e)
        {
            ShowBalloon();
        }
        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        private void btnBallon_Click(object sender, RoutedEventArgs e)
        {
            ShowBalloon();
        }
        private void miShutdownService_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        private void miGetServiceStatus_Click(object sender, RoutedEventArgs e)
        {
            ShowBalloon();
        }
    }
}
